package com.junqi.msg.mq;

import com.junqi.common.constant.MQConstant;
import com.junqi.common.entity.Message;
import com.junqi.common.entity.RedisKeyManager;
import com.junqi.msg.event.UserPushMsgEvent;
import com.junqi.msg.utils.RedisUtil;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;


@Component
@RocketMQMessageListener(topic = MQConstant.SEND_MSG_TOPIC,consumerGroup = MQConstant.SEND_MSG_GROUP, messageModel = MessageModel.BROADCASTING)
public class ChatMsgMQComsumer implements RocketMQListener<Message> {


    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void onMessage(Message dto) {

        String roomId = dto.getRoomId();
        //新消息入消息队列 使用zset有序集合存储消息
        RedisUtil.zAdd(RedisKeyManager.generateKey(RedisKeyManager.ROOM_MSG,roomId),dto,Double.valueOf(dto.getSendTime()));

        //有新消息入队，启动监听触发消息通知
        applicationEventPublisher.publishEvent(new UserPushMsgEvent(this, roomId));
    }

}
